import Vue from 'vue'
import Vuex from 'vuex'
// import router from './router'
Vue.use(Vuex)
const store = new Vuex.Store({
    state:{
        //存储的一些数据
        array:[],
        userObj:{},
        pageData:{},
        tabData:[]
    },
    getters:{
        //类似计算属性，返回值根据他的以来被缓存起来，只有当依赖值改变时才会重新计算
        getFilterArray(state){
            return state.array.map((item)=>item>10);
        },
        getUser(state){
            return state.userObj
        },
        getPageData(state){
            return state.pageData
        },
        getTabData(state){
            return state.tabData
        }
    },
    mutations:{
        setArray(state){
            state.array = [1,2,55,677,1];
        },
        addArray(state){
            state.array = state.array.map((item)=>item);
            alert(state.array)
        },
        setUserObj(state,data){
            if(data){
                state.userObj = data
            }else if(data == ""){
                sessionStorage.setItem("user","")
                state.userObj = {}
            }
            console.log('sdsd',data)
        },
        setPageData(state,data){
            if(data){
                state.pageData = data
            }
        },
        setTabData(state,data){
            if(data){
                state.tabData = data
            }
        }
    },
    actions:{
        setUser({commit},user){
            commit('setUserObj',user)
        },
        setTab({commit},data){
            commit('setTabData',data)
        },
        setPage({commit},data){
            commit('setPageData',data)
        }
    }
})
export default store;